<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script src="jdataview.js"></script>
  <script src="jbinary.js"></script>
  <script src="full.js"></script>
  <script>
    const converter = OpenCC.Converter({ from: 'hk', to: 'cn' });
    console.log(converter('漢語')); // output: 汉语

    // 总结
    // 这段代码的主要作用是从 ranger.idx32 和 ranger.grp32 两个文件中读取和解析角色数据。

    // ranger.idx32 用来存储每个数据块的偏移量和长度信息。

    // ranger.grp32 包含实际的角色数据，这些数据会根据 ranger.idx32 中的偏移量信息来提取，并通过 jBinary 工具解析为 JavaScript 对象。

    // 最终，解析出的角色数据被存储在 roles 数组中，供后续使用。

    // 获取数据
    async function fetchData() {
      let offset = [0]
      let length_arr = []
      let idx = 1

      let res1 = await fetch('./save/ranger.idx32')
      let arrayBuffer1 = await res1.arrayBuffer()

      // 这句代码干什么不清楚？？？
      let data_idx = new Uint32Array(arrayBuffer1)
      for (let i = 0; i < arrayBuffer1.byteLength; i++) {
        let offset_i = data_idx[i]
        if (offset_i) {
          offset[i + 1] = offset_i
        }
        let length_diff = offset[i + 1] - offset[i]
        if (length_diff) {
          length_arr[i] = length_diff
        }
      }

      //
      let jbinary_type = {
        'jBinary.all': 'name',
        'jBinary.littleEndian': true,
        ItemList: {
          item_id: 'int32',
          count: 'int32',
        },
        name: {
          ID: 'int32',
          HeadID: 'int32',
          IncLife: 'int32',
          UnUse: 'int32',
          Name: ['string', 20, 'big5'],
          Nick: ['string', 20, 'big5'],
          Sexual: 'int32',
          Level: 'int32',
          Exp: 'int32',
          HP: 'int32',
          MaxHP: 'int32',
          Hurt: 'int32',
          Poison: 'int32',
          PhysicalPower: 'int32',
          ExpForMakeItem: 'int32',
          Equip0: 'int32',
          Equip1: 'int32',
          Frame: ['array', 'int32', 15],
          MPType: 'int32',
          MP: 'int32',
          MaxMP: 'int32',
          Attack: 'int32',
          Speed: 'int32',
          Defence: 'int32',
          Medcine: 'int32',
          UsePoison: 'int32',
          Detoxification: 'int32',
          AntiPoison: 'int32',
          Fist: 'int32',
          Sword: 'int32',
          Knife: 'int32',
          Unusual: 'int32',
          HiddenWeapon: 'int32',
          Knowledge: 'int32',
          Morality: 'int32',
          AttackWithPoison: 'int32',
          AttackTwice: 'int32',
          Fame: 'int32',
          IQ: 'int32',
          PracticeItem: 'int32',
          ExpForItem: 'int32',
          MagicID: ['array', 'int32', 10],
          MagicLevel: ['array', 'int32', 10],
          TakingItem: ['array', 'int32', 4],
          TakingItemCount: ['array', 'int32', 4],
        },
      }

      // { data_length: 364, data_class: 'role', data_schema: schema.roleSave },

      let length_one = 364 //data_length
      let count = length_arr[idx] / length_one
      let grp_start = offset[idx]
      let res2 = await fetch('./save/ranger.grp32')
      let savefile = await res2.arrayBuffer()

      let roles = []
      for (let i = 0; i < count; i++) {
        let dataview = new jDataView(savefile, grp_start + i * length_one, length_one, true)
        let _jBinary = new jBinary(dataview, jbinary_type)
        let jdata = _jBinary.readAll()

        //繁体转中文
        //去除\u0000符号
        jdata.Name = converter(jdata.Name).replace(/\u0000/g, '')
        jdata.Nick = converter(jdata.Nick).replace(/\u0000/g, '')

        roles.push(jdata)
      }

      let jsonString = JSON.stringify(roles);
      console.log(jsonString);

      //取第一个
      let newRole = {
        Team: 0,
        FaceTowards: 0,
        Dead: 0,
        Step: 0,
        ExpGot: 0,
        Auto: 0,
        FightFrame: [],
        FightingFrame: 0,
        Moved: 0,
        Acted: 0,
        ActTeam: 0,
        ShowString: '',
        role_magic_count: 10,
        magicID: [],
        position_layer: {},
        AI_MoveX: null,
        AI_MoveY: null,
        AI_ActionX: null,
        AI_ActionY: null,
        AI_Magic: null,
        AI_Item: null,
        prevX: null,
        prevY: null,
        AI_Action: 0,
        dead_alpha: 255,
      }

      console.log(roles[0])
      newRole.data = roles[0]

      newRole.j = {
        centerx: 384,
        centery: 240,
        win_height: 640,
        win_width: 1024,
        mainmap: {},
        op: {
          MaxLevel: 30,
          MaxHP: 999,
          MaxMP: 999,
          MaxPhysicalPower: 100,
          MaxPoison: 100,
          MaxAttack: 100,
          MaxDefence: 100,
          MaxSpeed: 100,
          MaxMedcine: 100,
          MaxUsePoison: 100,
          MaxDetoxification: 100,
          MaxAntiPoison: 100,
          MaxFist: 100,
          MaxSword: 100,
          MaxKnife: 100,
          MaxUnusual: 100,
          MaxHiddenWeapon: 100,
          MaxKnowledge: 100,
          MaxMorality: 100,
          MaxAttackWithPoison: 100,
          MaxFame: 999,
          MaxIQ: 100,
          MaxExp: 99999,
          MoneyItemID: 174,
          CompassItemID: 182,
        },
      }

      console.log(newRole)
    }

    fetchData()
  </script>
</body>

</html>